package com.hzchendou.demo.coupon;

/**
 * 优惠券编码工具类
 *
 * @Date: 2022-06-14 15:31
 * @since: 1.0
 */
public class CouponCodeUtils {


    private static final Long COUPON_ID_BIT_LEN = 15L;
    private static final Long REDEEM_SERIAL_NUM_BIT_LEN = 30L;
    /// checkSum 13位，前6位是1的个数
    private static final Long NUMBER_OF_ONE_BIT_LEN = 6L;
    private static final Long REMAINDER_BIT_LEN = 7L;
    private static final long CHECK_SUM_BIT_LEN = NUMBER_OF_ONE_BIT_LEN + REMAINDER_BIT_LEN;
    private static final long DIVISOR = 1L << REMAINDER_BIT_LEN;

    private static final long COUPON_SCHEME_ID_MASK = (1 << COUPON_ID_BIT_LEN) - 1;
    private static final long REDEEM_SERIAL_NUM_MASK =
            ((1L << REDEEM_SERIAL_NUM_BIT_LEN) - 1) << COUPON_ID_BIT_LEN;

    private static final long REMAINDER_MASK = (1L << REMAINDER_BIT_LEN) - 1;
    private static final long NUMBER_OF_ONE_MASK = ((1L << NUMBER_OF_ONE_BIT_LEN) - 1) << REMAINDER_BIT_LEN;

    private static final char[] r =
            new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
                    'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E',
                    'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
                    'X', 'Z', 'Y', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    private static final int l = r.length;

    private CouponCodeUtils() {
        throw new UnsupportedOperationException(
                "CouponCodeUtils is static class, can not be construct to a instance");
    }

    /**
     * 生成兑换码编码
     * -------------------------------------------------------
     * 30位兑换码序列     15位优惠方案id     13位校验位(前6 + 后7)
     * -------------------|--------------|--------------------|
     *
     * @param couponSchemeId
     * @param redeemSerialNum
     * @return
     */
    public static long enRedeemNum(long couponSchemeId, long redeemSerialNum) {
        redeemSerialNum = redeemSerialNum << COUPON_ID_BIT_LEN;
        long r = couponSchemeId | redeemSerialNum;
        long n = numOfOne(r);
        long re = r % DIVISOR;
        r = (r << NUMBER_OF_ONE_BIT_LEN) | n;
        r = (r << REMAINDER_BIT_LEN) | re;
        return r;
    }

    /**
     * 判断二进制中1的位数
     *
     * @param n
     * @return
     */
    private static long numOfOne(long n) {
        long count = 0;
        long seed = n;
        while (seed != 0) {
            seed = seed & (seed - 1);
            count++;
        }
        return count;
    }

    public static long[] deRedeemNum(long redeemNum) {
        redeemNum = redeemNum >> CHECK_SUM_BIT_LEN;
        long couponSchemeId = redeemNum & COUPON_SCHEME_ID_MASK;
        long redeemSerialNum = redeemNum & REDEEM_SERIAL_NUM_MASK;
        redeemSerialNum = redeemSerialNum >> COUPON_ID_BIT_LEN;
        return new long[]{couponSchemeId, redeemSerialNum};
    }

    public static boolean checkVaild(long redeemNum) {
        if (redeemNum > 0) {
            long checkSum = redeemNum & REMAINDER_MASK;
            long n = (redeemNum & NUMBER_OF_ONE_MASK) >> REMAINDER_BIT_LEN;
            long r = redeemNum >> CHECK_SUM_BIT_LEN;
            if (numOfOne(r) == n) {
                if (r % DIVISOR == checkSum) {
                    return Boolean.TRUE;
                }
            }
        }
        return Boolean.FALSE;
    }

    public static String enRedeemCode(long redeemNum) {
        char[] buf = new char[32];
        int charPos = 32;
        while ((redeemNum / l) > 0) {
            int ind = (int) (redeemNum % l);
            buf[--charPos] = r[ind];
            redeemNum /= l;
        }
        buf[--charPos] = r[(int) (redeemNum % l)];
        String str = new String(buf, charPos, (32 - charPos));
        return str;
    }

    public static long deRedeemCode(String redeemCode) {
        char chs[] = redeemCode.toCharArray();
        long res = 0L;
        for (int i = 0; i < chs.length; i++) {
            int ind = -1;
            for (int j = 0; j < l; j++) {
                if (chs[i] == r[j]) {
                    ind = j;
                    break;
                }
            }
            if (ind == -1) {
                return -1;
            }
            if (i > 0) {
                res = res * l + ind;
            } else {
                res = ind;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        long redeemNum = enRedeemNum(1, 10);
        long[] decodes = deRedeemNum(redeemNum);
        System.out.println(redeemNum);
        System.out.println(checkVaild(redeemNum));
        System.out.println(enRedeemCode(redeemNum));
        System.out.println(deRedeemCode(enRedeemCode(redeemNum)));
    }
}
